from lxml import etree
text = '''
<html>
    <div class="clearfix">
    <div class="nav_com">
     <ul>
       <li class="active">
<a href="/" rel="external nofollow" >推荐</a>
</li>
      <li class="">
<a href="/nav/python" rel="external nofollow" >Python</a>
</li>
      <li class="">
<a href="/nav/java" rel="external nofollow" >Java</a>
</li>
     </ul>
    </div>
    </div>
</html>>
</html>>
'''
html = etree.HTML (text)    # 将字符串解析为html文档
# print(html)
# 将字符串序列化为html
result = etree.tostring (html,encoding='utf-8').decode ('utf-8')
print (result)
